package com.moon.adapter;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.moon.R;
import com.moon.constants.MoonConstants;
import com.moon.model.Topic;
import com.moon.utils.BitmapImageCache;
import com.moon.utils.DateUtils;

/**
 * @author paul
 * @date 2014年5月17日
 */
public class TopicListAdapter extends ArrayAdapter<Topic> {

    private static final String TAG       = TopicListAdapter.class.getSimpleName();

    private LayoutInflater      inflater;
    public ImageLoader          imageLoader;

    //默认宽高
    int                         maxWidth  = 0;
    int                         maxHeight = 0;

    public TopicListAdapter(Context context, List<Topic> list) {
        super(context, 0, list);
        inflater = LayoutInflater.from(context);
        //imageLoader= new ImageLoader(context);
        //使用volley的ImageLoader
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(requestQueue, BitmapImageCache.getInstance());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Topic topic = getItem(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.topic_item_adapter, null);
            viewHolder.topicImage = (ImageView) convertView.findViewById(R.id.topic_image);
            viewHolder.topicTitle = (TextView) convertView.findViewById(R.id.topic_title);
            viewHolder.topicGmtCreate = (TextView) convertView.findViewById(R.id.topic_gmt_create);
            viewHolder.topicIntro = (TextView) convertView.findViewById(R.id.topic_intro);
            //viewHolder.replyCount = (TextView) convertView.findViewById(R.id.reply_count);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //使用TOPIC的第一张图片,但下载一张图片太大了 ,是否使用tag或是其它  先使用用户头像
        String topicPicPath = "";
        if (StringUtils.isNotBlank(topic.getImages())) {
            String[] images = topic.getImages().split("\\|");
            topicPicPath = MoonConstants.MOON_DOMAIN_STATIC_TOPIC + images[0];
            //            imageLoader.DisplayImage(MoonConstants.MOON_DOMAIN_STATIC_TOPIC  + imagePath, viewHolder.topicImage);
        } else {
            topicPicPath = MoonConstants.MOON_DOMAIN_STATIC_TOPIC_SMALL + topic.getImgUrl();
        }

        //加载图片
        ImageListener imageListener = ImageLoader.getImageListener(viewHolder.topicImage,
                0, R.drawable.app_panel_pic_icon);
        imageLoader.get(topicPicPath, imageListener, maxWidth, maxHeight);

        //            imageLoader.DisplayImage(MoonConstants.MOON_DOMAIN_STATIC_HEAD + topic.getImgUrl(),
        //                    viewHolder.topicImage);

        viewHolder.topicTitle.setText(topic.getTitle());
        viewHolder.topicIntro.setText(topic.getIntro());
        viewHolder.topicGmtCreate.setText(DateUtils.formatDate2String(topic.getGmtCreate(),
                DateUtils.DATEPATTERN_TIME));
        //viewHolder.replyCount.setText(String.valueOf(topic.getReplyCount()));

        //        TextView name = (TextView) convertView.findViewById(R.id.item_name);
        //        name.setText(info.getName());

        return convertView;
    }

    class ViewHolder {
        ImageView topicImage;
        TextView  topicTitle;
        TextView  topicGmtCreate;
        TextView  topicIntro;
        TextView  replyCount;
    }

    @Override
    public Topic getItem(int position) {
        return super.getItem(position);
    }

}
